﻿using DLL.NET70.Infrastructure.EventAggregator;
using DLL.NET70.Infrastructure.Prism;
using DLL.Standard.Infrastructure.Entity;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Net60.MotionCtrl.ControlBase;
using System.Windows;
using DLL.Standard.Infrastructure.Log;
using DLL.WCL.UserWindow;
using System;
using DLL.NET.Login.Account.Entity.DTO;
using Prism.Ioc;
using DLL.NET70.Infrastructure.Prism.EventAggregator;
using DLL.NET50.DB.PIVAS.ORM.Service;
using System.Linq;
using System.Threading;

namespace MSDM.ViewModels
{
    public class MainWindowViewModel : CMVVMBase
    {
        private string _LogMessage = "Prism Application";

        public string ShowLogMessage
        {
            get { return _LogMessage; }
            set { SetProperty(ref _LogMessage, value); }
        }

        private string _logForground = "Green";

        public string LogForeground
        {
            get { return _logForground; }
            set { SetProperty(ref _logForground, value); }
        }

        //InjectorCount
        private string _InjectorCount = "";

        public string InjectorCount
        {
            get { return _InjectorCount; }
            set { SetProperty(ref _InjectorCount, value); }
        }

        private IRegionManager _regionManager;

        private IEventAggregator _eventAggregator;
        private IMotionCtrlBase _nMotionCtrl;
        private IDialogService _dialogService;
        private IContainerExtension _container;
        private ISendCommandMessage _sendCommandMessage;
        private adi_accountDTO _logAccount;

        public MainWindowViewModel(IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IApplicationCommands applicationCommands,
            IContainerExtension container,
            IMotionCtrlBase motionCtrlBase,
            IDialogService dialogService,
            IPivasAccountService pivasAccountService,
            ISendCommandMessage sendCommandMessage
            ) : base(regionManager, eventAggregator, applicationCommands)
        {
            _regionManager = regionManager;
            ApplicationCommands = applicationCommands;
            _eventAggregator = eventAggregator;
            _dialogService = dialogService;
            _nMotionCtrl = motionCtrlBase;
            _container = container;
            _sendCommandMessage = sendCommandMessage;
            CloseCommand = new DelegateCommand(Close);
            Title = "多工位智能配药机";
            _logAccount = _container.Resolve<adi_accountDTO>();
            var objs = pivasAccountService.GetAllAcount();
        }

        public DelegateCommand CloseCommand { get; set; }

        private void Close()
        {
            //

            _sendCommandMessage.Publish(GlobalCommand.ExitApp);
            _nMotionCtrl.Disconnect();
        }

        public override void Loaded(Window window)
        {
            //注册消息接收
            RegisterLogMsgReceive();
            RegisterCommandMsgReceive();
            SendCommandMessage(GlobalCommand.UpdateNavigateBtn);
            Title = "多工位配药机器人(员工号：" + _logAccount.MJobNumber + ")";
        }

        public override void UnLoaded()
        {
            //注销接收消息事件
            UnRegisterLogMsgReceive();
            UnRegisterCommandMsgReceive();
        }

        private static void OnDialogClosed(IDialogResult result)
        {
            var bresult = result as DialogResult;
        }

        public override void ReceiveLogMsg(CLogMessage Message)
        {
            ReceiveLogMessage(Message);
        }

        private void ReceiveLogMessage(CLogMessage msg)
        {
            switch (msg.Level)
            {
                case 0:
                    {
                        LogForeground = "Green";
                        break;
                    }

                case (int)LogLevel.Debug:
                    {
                        LogForeground = "Green";
                        break;
                    }
                case (int)LogLevel.Information:
                    {
                        LogForeground = "Green";
                        break;
                    }
                case (int)LogLevel.Warning:
                    {
                        LogForeground = "Orange";
                        break;
                    }
                case (int)LogLevel.Error:
                    {
                        LogForeground = "Red";
                        break;
                    }

                default:
                    {
                        LogForeground = "Green";
                        break;
                    }
            }

            ShowLogMessage = msg.Message;
        }

        public override void CommandMessageReceived(CCommandMessage message)
        {
            if (message.CommandStr == GlobalCommand.ChangeAccount)
            {
                Title = "多工位配药机器人(员工号：" + _logAccount.MJobNumber + ")";
            }
            else if (message.CommandStr == GlobalCommand.BindingInjector)
            {
                InjectorCount = message.value;
            }
            else if ("RegisterInjectorOk" == message.CommandStr)
            {
                try
                {
                    string msg = message.value;
                    string[] msg2 = msg.Split("-");
                    int AllowCount = Convert.ToInt32(msg2[0]);
                    int CurrentUsedCount = Convert.ToInt32(msg2[1]);
                    InjectorCount = "总量=" + AllowCount.ToString() + ";使用量=" + CurrentUsedCount.ToString();
                }
                catch (Exception e)
                {
                    LogMessage("注射器绑定出现异常：" + e.Message, LogLevel.Warning);
                }
            }
            base.CommandMessageReceived(message);
        }

        private void SendCommandMsg(string cmd, string value = "")
        {
            CCommandMessage msg = new CCommandMessage();
            msg.CommandStr = cmd;
            msg.value = value;
            _eventAggregator.GetEvent<CCommandMessageEvent>().Publish(msg);
        }

        #region menu

        private DelegateCommand _AuthorizationCommand;

        public DelegateCommand AuthorizationCommand =>
            _AuthorizationCommand ?? (_AuthorizationCommand = new DelegateCommand(Authorization));

        private void Authorization()
        {
            //_regionManager.RequestNavigate("ContentRegion", "PUC_AccountAuthorization");
            _dialogService.ShowDialog("Dialog_AccountAuthorization", null, null, "CustomDialogWindow");
        }

        private DelegateCommand _menuLightCommand;

        public DelegateCommand MenuLightCommand =>
            _menuLightCommand ?? (_menuLightCommand = new DelegateCommand(ExecuteMenuLight));

        private void ExecuteMenuLight()
        {
            //_regionManager.RequestNavigate(CRegionNames.MainContentRegion, "PaletteSelector");
            _dialogService.ShowDialog("Dialog_PaletteSelector", null, null, "CustomDialogWindow");
            return;
        }

        private DelegateCommand _injectorAuthorizationCommand;

        public DelegateCommand InjectorAuthorizationCommand =>
            _injectorAuthorizationCommand ?? (_injectorAuthorizationCommand = new DelegateCommand(InjectorAuthorization));

        private void InjectorAuthorization()
        {
            // 菜单项被点击时的逻辑
            // CDialogExtension.ShowYesNo(_dialogService, "ce shi ", OnDialogClosed);
            _dialogService.Show("PUC_InjectorAuthorize", null, null, GlobalDialogNames.CustomDialogWindow);
            //string str = "";
        }

        #endregion menu
    }
}